package core;

import android.content.Context;
import foc.physics.solver.R;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

public class Global {
    public static HashMap<String, Var> values;
    public static HashMap<String, Action> operations;
    /**
     * In the form (problem_type_string, formula_list)
     */
    public static HashMap<String, ArrayList<Formula>> formulas;

    /**
     * In the form (problem_type_string, problem_type)
     */
    public static HashMap<String, ProblemType> problems;

    public static HashMap<String, Integer> units;
    public static HashMap<String, Integer> conv;



    static {
        values = new HashMap<String, Var>();
        operations = new HashMap<String, Action>();
        formulas = new HashMap<String, ArrayList<Formula>>();
        problems = new HashMap<String, ProblemType>();
        conv = new HashMap<String, Integer>();
        units = new HashMap<String, Integer>();
        loadConstants();
        loadOperations();
        loadUnits();
    }

    private static void loadUnits() {
        units.put("velocity", R.array.velocity_units);
        conv.put("velocity", R.array.velocity_conversion);
        units.put("time", R.array.time_units);
        conv.put("time", R.array.time_conversion);
        units.put("acceleration", R.array.acceleration_units);
        conv.put("acceleration", R.array.acceleration_conversion);
        units.put("distance", R.array.distance_units);
        conv.put("distance", R.array.distance_conversion);
    }

    private static void loadConstants() {
        //TODO: add constants
    }

    public static void loadFormulas(final Context context) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(context.getResources()
                    .getAssets().open("formulas.txt")));
            String input;
            br.readLine();
            while ((input = br.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(input, ":~");
                String key = st.nextToken().trim();
                if (!formulas.containsKey(key))
                    formulas.put(key, new ArrayList<Formula>());
                formulas.get(key).add(
                        new Formula(st.nextToken(),
                                st.nextToken(),
                                st.nextToken().trim()));

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadProblems(final Context context) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(context.getResources()
                    .getAssets().open("types.txt")));
            String key;
            br.readLine();
            while ((key = br.readLine()) != null) {

                problems.put(key, new ProblemType(Integer.parseInt(br.readLine()),
                        br.readLine(),
                        br.readLine(),
                        br.readLine()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static void loadOperations() {
        operations.put("+", new Action() {
            public double value(double b, double a) {
                return a + b;
            }
        });
        operations.put("-", new Action() {
            public double value(double b, double a) {
                return a - b;
            }
        });
        operations.put("*", new Action() {
            public double value(double b, double a) {
                return a * b;
            }
        });
        operations.put("/", new Action() {
            public double value(double b, double a) {
                return a / b;
            }
        });
        operations.put("^", new Action() {
            public double value(double b, double a) {
                return Math.pow(a, b);
            }
        });
    }

    //TODO:make sure variables don't disappear by clearing too early (potential future bug)
    public static void clearVariables() {
        values.clear();
    }
}